import { ref, computed } from "vue";
import { defineStore } from "pinia";
import { getContents,getContentById,getContentsByTypeId} from "@/services/content";
import {deleteFiles} from "@/services/index";
// import { getSysDictsByType } from "@/services/sysDict";

export const useContentStore = defineStore("content", () => {
    const content = ref({type:{}});
    const contents = ref({});
    const fileList = ref([]);
    const updateDialogVisible = ref(false);
    const contentTypes = ref([]);
    const search = ref({});
    const removeFileList = ref([]);
    function setUpdateDialogVisible(visible) {
        updateDialogVisible.value = visible;
    }
    async function fetchContentById(id) {
        let {data} = await getContentById(id);
        content.value = data;
        fileList.value = data.images.map(item => ({url:item.path}));
        console.log("fileList",fileList);
    }
    async function fetchContents(params,searchParams) {
        if(searchParams){
            search.value = searchParams;
        }
           
        params = {...params,...search.value}
        let {data} = await getContents(params);
        contents.value = data;
    }
    async function fetchContentsByTypeId(params,searchParams) {
        if(searchParams){
            search.value = searchParams;
        }
           
        params = {...params,...search.value}
        let {data} = await getContentsByTypeId(params);
        contents.value = data;
    }
    async function fetchContentsOrder(typeId) {
        // let data = await getContents();
        // contents.value = data;
    }
    async function fetchContentTypes() {
        // let data = await getSysDictsByType(1);
        // contentTypes.value = data;
    }
    function fetchDeleteFiles(){
        deleteFiles(removeFileList.value);
        removeFileList.value = [];
    }
    return {
        fileList,
        removeFileList,
        contents,
        content,
        updateDialogVisible,
        contentTypes,
        fetchContents,
        setUpdateDialogVisible,
        fetchContentById,
        fetchContentsByTypeId,
        fetchDeleteFiles,
        fetchContentsOrder
    };
});
